package com.wy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wy.common.Result;
import com.wy.dto.GoodsAuctionDto;
import com.wy.entity.GoodsOrder;
import com.wy.entity.UserInfo;
import com.wy.service.GoodsOrderService;
import com.wy.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:王渝
 * @Date: 2023/1/4  15:09
 * @Version 1.0
 */
@RequestMapping("/order")
@Slf4j
@RestController
public class GoodsOrderController {

    @Autowired
    private GoodsOrderService goodsOrderService;

    @Autowired
    private UserInfoService userInfoService;
    /**
     * 分页查询用户参与的拍卖
     * @param currentPage 当前页
     * @param pageSize 一页大小
     * @param status 状态（1待付款 2已付款 3未支付 4已退款）默认1
     * @param userId 用户id
     * @return
     */
    @RequestMapping("/getAll/{currentPage}/{pageSize}/{status}/{userId}")
    public Result<PageInfo<GoodsOrder>> queryAllInfoByStatusAndUserId(@PathVariable int currentPage,
                                                                      @PathVariable int pageSize,
                                                                      @PathVariable String status,
                                                                      @PathVariable Long userId){
        PageInfo<GoodsOrder> pageInfo = null;
        if("1".equals(status)){
            pageInfo = goodsOrderService.queryPageByUserId(currentPage, pageSize,userId, status);
        }else {
            pageInfo = goodsOrderService.queryPageByUserId2(currentPage, pageSize,userId, status);
        }

        return Result.success(pageInfo);
    }


    /**
     * 支付
     * @param goodsOrder 订单信息
     * @return
     */
    @RequestMapping("/pay")
    public Result<String> payOrder(@RequestBody GoodsOrder goodsOrder){

        LambdaQueryWrapper<GoodsOrder> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(GoodsOrder::getOrderId,goodsOrder.getOrderId());
        GoodsOrder one1 = goodsOrderService.getOne(lambdaQueryWrapper1);
        if("2".equals(one1.getStatus())){
            return Result.error("已经付款");
        }

        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserInfo::getUserId,goodsOrder.getBuyerUserId());
        lambdaQueryWrapper.select(UserInfo::getBalance);
        UserInfo one = userInfoService.getOne(lambdaQueryWrapper);
        if(one.getBalance().compareTo(goodsOrder.getTransactionAmount()) < 0) {
            return Result.error("余额不足");
        }
        boolean b = goodsOrderService.payOrder(goodsOrder);
        if(b){
            return Result.success("支付成功");
        }
        return Result.error("支付失败");
    }

    /**
     * 分页查询用户卖出的商品
     * @param currentPage 当前页
     * @param pageSize 一页大小
     * @param userId 用户id
     * @param status 1未发货 2已发货 3已收货
     * @return
     */
    @RequestMapping("/getSell/{currentPage}/{pageSize}/{status}/{userId}")
    public Result<PageInfo<GoodsOrder>> getSell(@PathVariable int currentPage, @PathVariable int pageSize,@PathVariable Long userId,@PathVariable String status){
        PageInfo<GoodsOrder> pageInfo = null;
        if("1".equals(status)){
            pageInfo = goodsOrderService.queryPageBySellerId(currentPage, pageSize,userId);
        }else if("2".equals(status)){
            pageInfo = goodsOrderService.queryPageBySellerId2(currentPage, pageSize,userId);
        }else {
            pageInfo = goodsOrderService.queryPageBySellerId3(currentPage, pageSize,userId);
        }
        return Result.success(pageInfo);
    }



}
